package com.dlx.mem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlx.common.exception.SmmsException;
import com.dlx.common.result.ResultCodeEnum;
import com.dlx.common.utils.MD5;
import com.dlx.mem.mapper.MemInfoMapper;
import com.dlx.mem.service.MemInfoService;
import com.dlx.mem.service.MemLevelService;
import com.dlx.model.mem.MemInfo;
import com.dlx.vo.mem.MemInfoQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author nglon
 * @since 2021-11-08
 */
@Service
public class MemInfoServiceImpl extends ServiceImpl<MemInfoMapper, MemInfo> implements MemInfoService {

    @Autowired
    private MemLevelService memLevelService;

    @Override
    public IPage<MemInfo> selectMemInfoPage(Page<MemInfo> pageMemInfo, MemInfoQueryVo memInfoQueryVo) {
        QueryWrapper<MemInfo> wrapper = new QueryWrapper<>();
        if (memInfoQueryVo.getId() != null) {
            wrapper.like("id", memInfoQueryVo.getId());
        }
        if (memInfoQueryVo.getMobile() != null) {
            wrapper.like("mobile", memInfoQueryVo.getMobile());
        }
        if (memInfoQueryVo.getNickname() != null) {
            wrapper.like("nickname", memInfoQueryVo.getNickname());
        }
        if (memInfoQueryVo.getSex() != null) {
            wrapper.eq("sex", memInfoQueryVo.getSex());
        }
        if (memInfoQueryVo.getIsDisabled() != null) {
            wrapper.eq("is_disabled", memInfoQueryVo.getIsDisabled());
        }
        IPage<MemInfo> memInfoPage = this.baseMapper.selectPage(pageMemInfo, wrapper);
        memInfoPage.getRecords().forEach(this::packageMemInfo);
        return memInfoPage;
    }

    @Override
    public void add(MemInfo memInfo) {
        QueryWrapper<MemInfo> wrapper = new QueryWrapper<>();
        memInfo.setPassword(MD5.encrypt(memInfo.getPassword()));
        if (!memInfo.getMobile().isEmpty()) {
            wrapper.eq("mobile", memInfo.getMobile());
        }
        if (baseMapper.selectOne(wrapper) != null) {
            throw new SmmsException(ResultCodeEnum.MEMINFO_ADD_ERROR);
        }
        baseMapper.insert(memInfo);
    }

    @Override
    public MemInfo getByUserId(Long userId) {
        QueryWrapper<MemInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        MemInfo memInfo = this.baseMapper.selectOne(wrapper);
        this.packageMemInfo(memInfo);
        return memInfo;
    }

    @Override
    public Boolean updateByUserId(MemInfo memInfo) {
        QueryWrapper<MemInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", memInfo.getUserId());
        int update = this.baseMapper.update(memInfo, wrapper);
        return update > 0;
    }

    @Override
    public MemInfo getById(Long id) {
        MemInfo memInfo = this.baseMapper.selectById(id);
        this.packageMemInfo(memInfo);
        return memInfo;
    }

    @Override
    public MemInfo show(Long memInfoId) {
        return this.packageMemInfo(this.baseMapper.selectById(memInfoId));
    }

    @Override
    public void lock(Long memInfoId, Boolean isDisabled) {
        MemInfo memInfo = this.getById(memInfoId);
        memInfo.setIsDisabled(isDisabled);
        this.updateById(memInfo);
    }

    private MemInfo packageMemInfo(MemInfo memInfo) {
        //处理会员等级Id
        memInfo.getParam().put("memLevelString", this.memLevelService.getById(memInfo.getLevelId()).getLevel());
        //处理会员账号状态
        String isDisabled = memInfo.getIsDisabled() ? "已禁用" : "正常";
        memInfo.getParam().put("isDisabledString", isDisabled);
        //处理会员性别
        String sex = memInfo.getSex() ? "男" : "女";
        memInfo.getParam().put("sexString", sex);
        return memInfo;
    }
}
